package com.itheima.service.impl;

import cn.hutool.core.util.ArrayUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.domain.Product;
import com.itheima.mapper.ProductMapper;
import com.itheima.service.IProductService;
import com.itheima.vo.PageBeanVo;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 商品信息 服务实现类
 * </p>
 *
 * @author fukeke
 * @since 2022-11-11
 */
@DubboService
public class ProductServiceImpl extends ServiceImpl<ProductMapper, Product> implements IProductService {
    @Autowired
    private IProductService iProductService;

    @Override

    public Product findById(Long id) {
        Product product = iProductService.findById(id);
        return product;
    }

    @Autowired
    ProductMapper productMapper;

    @Override
    public PageBeanVo findProductByPage(Integer pageNum, Integer pageSize, Integer brandId, String keyword, Integer productCategoryId, String productSn, Integer publishStatus) {
        LambdaQueryWrapper<Product> lqw=new LambdaQueryWrapper<>();
        Page<Product> page=new Page<>(pageNum,pageSize);

        lqw.eq(brandId!=null,Product::getBrandId,brandId);
        lqw.like(keyword!=null,Product::getKeywords,keyword);
        lqw.eq(productCategoryId!=null,Product::getProductCategoryId,productCategoryId);
        lqw.like(productSn!=null,Product::getProductSn,productSn);
        lqw.eq(publishStatus!=null,Product::getPublishStatus,publishStatus);
        lqw.orderByDesc(Product::getSort);

        productMapper.selectPage(page, lqw);
        return new PageBeanVo(pageNum,pageSize,page.getTotal(),page.getRecords());

    }

    @Override
    public void createProduct(Product productParam) {
        productMapper.insert(productParam);
    }

    @Override
    public Product findByNameOrId(String keyword) {
        LambdaQueryWrapper<Product>lqw=new LambdaQueryWrapper<>();
        lqw.like(keyword!=null,Product::getKeywords,keyword);
        Product product = productMapper.selectOne(lqw);
        return product;
    }

    @Override
    public void updateDeleteStatus(Integer[] ids, Integer deleteStatus) {
        if(ArrayUtil.isNotEmpty(ids))
        {
            for (Integer id : ids) {
                Product product = productMapper.selectById(id);
                product.setDeleteStatus(deleteStatus);
                productMapper.updateById(product);

            }
        }
    }

    @Override
    public void setNew(Integer[] ids, Integer newStatus) {
        if(ArrayUtil.isNotEmpty(ids))
        {
            for (Integer id : ids) {
                Product product = productMapper.selectById(id);
                product.setNewStatus(newStatus);
                productMapper.updateById(product);

            }
        }
    }

    @Override
    public void upAndDown(Integer[] ids, Integer publishStatus) {
        if(ArrayUtil.isNotEmpty(ids))
        {
            for (Integer id : ids) {
                Product product = productMapper.selectById(id);
                product.setNewStatus(publishStatus);
                productMapper.updateById(product);

            }
        }
    }

    @Override
    public void recommend(Integer[] ids, Integer recommendStatus) {
        if(ArrayUtil.isNotEmpty(ids))
        {
            for (Integer id : ids) {
                Product product = productMapper.selectById(id);
                product.setNewStatus(recommendStatus);
                productMapper.updateById(product);

            }
        }
    }


    @Override
    public void updateProduct(Integer id, Product productParam) {

        Long id1 = productParam.getId();

        Product product = productMapper.selectById(id);
        product.setId(id1);

        productMapper.updateById(product);
    }

    @Override
    public Product selectById(Long id) {
        return productMapper.selectById(id);
    }
}
